<template>
	<div>
		<Row>
	        <Col span="24"  style="text-align: center;">
	            <Card style="width:880px;margin: auto;">
	            	<!--加载中样式-->
	            	<Spin size="large" fix v-if="loadingShow"></Spin>
	                
	                <p slot="title"><slot></slot></p>
	                <Row>
				        <Col span="12" >
				        	<span>选择年级：</span>
							<Select :clearable=true v-model="grade" style="width:200px" @on-change="gradeSelectChange">
						        <Option v-for="item in gradeList" :value="item.value" :key="item.value">{{ item.label }}</Option>
						    </Select>
				        </Col>
				        <Col span="12">
				        	<span>年级属性：</span>
							<Select v-model="subject" style="width:200px">
						        <Option v-for="item in subjectList" :value="item.value" :key="item.value">{{ item.label }}</Option>
						    </Select>
				        </Col>
				    </Row>
				    <Row style="margin-top:15px;">
				        <Col span="12" >
				        	<span>入学年份：</span>
							<!--<Input  v-model="startgrade" placeholder="请输入入学年份" style="width:200px"></Input>-->
							<Select v-model="startgrade" style="width:200px">
						        <Option v-for="item in startgradeList" :value="item.value" :key="item.value">{{ item.label }}</Option>
						    </Select>
				        </Col>
				       
				    </Row>
				    <Row style="margin-top:15px">
				    	<Col span="22" offset="2" style="text-align: left;">
				    		<span>选择科目：</span>
				    		<CheckboxGroup v-for="item in ninesubjectlist" :key="item.value" v-model="selectsubject" :label="item.value" @on-change="subjectboxchange(item.value,item.label)">
						        <Checkbox :label="item.value">{{item.label}}</Checkbox>
						    </CheckboxGroup>
				    	</Col>
				    </Row>
				    <Row style="margin-top:15px">
				    	<Col span="3" offset="2" style="text-align: left;">
				    		<span>科目满分设置：</span>
				    	</Col>
				    </Row>
				    <Row >
				    	<Col v-for="(item,index) in selectsbujectandscore" :key="item.subject" span="8"  style="text-align: center;margin-top:10px">
				    		<span>{{item.label}}:</span>
				    		<Input placeholder="满分值" v-model="selectsbujectandscore[index].score" style="width: 55px" />
				    	</Col>
				    </Row>
				    <Row>
				    	<Col span="24" style="margin-top:15px">
				    		<Button type="ghost" @click="chance" icon="close-round">取消</Button>
				    		<Button type="primary" @click="savegrade" icon="checkmark-round">保存</Button>
				    	</Col>
				    </Row>
	            </Card>
	        </Col>
	    </Row>
	    
	</div>
</template>

<script>
	import api from "@/components/js/Apidomain.js"
	export default{
		props: {
	      	importeditid:"",
	   },
		data(){
			return{
				loadingShow:false,		//控制加载中显示和隐藏
				
				startgrade:"",
//				startgrade:String(new Date().getFullYear()),		//入学年份
				grade:"",				//年级
				subject:"",				//文理科
				selectsubject:[],		//选择上课的科目
				
				selectsbujectandscore:[],
				subjectList: [],
				gradeList: [],
                ninesubjectlist: [],
                startgradeList:[]
			}
		},
		created(){
			this.getgrademenu()
			this.editgrade();
		},
		methods:{
			subjectboxchange:function(value,label){
				//默认数组里面没有传进来的value值，通过判断如果有值，就把arrhasvalue置为true
				var arrhasvalue = false;
				
				for(var i = 0;i<this.selectsbujectandscore.length;i++){
					if(this.selectsbujectandscore[i].subject == value){
						this.selectsbujectandscore.splice(i,1);
						arrhasvalue = true;
					}
				}
				if(arrhasvalue == false){
					this.selectsbujectandscore.push({subject:value,label:label,score:""})
				}
			},
			//获取增加年级面板的菜单
			getgrademenu:function(){
				this.loadingShow = true;
				var self = this;
            	let url = api.apidomain+'/tqes/web/tchGrade/getGradeSelectParamInfo';
            	this.$http.post(url, {}).then(function(response) {
					if(response.data.success == true){
//						//console.log(response.data.data.gradeList)
						self.subjectList = response.data.data.subjectList;
						self.gradeList = response.data.data.gradeList;
						self.ninesubjectlist = response.data.data.nineSubjectList
						self.startgradeList = response.data.data.enrolYearSelect
	            	}else if(response.data.success == false){
//		            		//console.log(response.data.msg)
	            		self.$Message.error({
							content: response.data.msg,
							duration: 10,
							closable: true
						});	
	            	}
	            	self.loadingShow = false;
				})
				.catch(function(error) {
					self.$Message.error({
							content: '系统发生错误：错误码00004',
							duration: 10,
							closable: true
						});
				});
			},
			gradeSelectChange:function(){
				var self = this;
            	let url = api.apidomain+'/tqes/web/tchGrade/getChooseSchoolYear';
            	this.$http.post(url, {"gradeNo":this.grade}).then(function(response) {
					if(response.data.success == true){
//						console.log(response.data.data)
						self.startgrade = response.data.data
	            	}else if(response.data.success == false){
//		            		//console.log(response.data.msg)
	            		self.$Message.error({
							content: response.data.msg,
							duration: 10,
							closable: true
						});	
	            	}
				})
				.catch(function(error) {
					self.$Message.error({
							content: '系统发生错误：错误码00005',
							duration: 10,
							closable: true
						});
				});
			},
			chance:function(){
				this.$emit('chance')
			},
			savegrade:function(){
				this.loadingShow = true;
				//判断importeditid，如果为空，则为新增，如果有值则带值调接口为修改
				if(this.importeditid == ""){
					var gradedata = 
						{'enrolYear':this.startgrade,
							
						'id':"",
						'no':this.grade,			//年级
						'name':"",
						'deptId':"",
						'orderNo':"",
						'isBranch':this.subject,	//分科信息	
						'selectSubject':this.selectsubject,
						'selectSubjectAndScore':this.selectsbujectandscore
						}
				}else{
					var gradedata = 
						{'enrolYear':this.startgrade,
							
						'id':this.importeditid,
						'no':this.grade,			//年级
						'name':"",
						'deptId':"",
						'orderNo':"",
						'isBranch':this.subject,	//分科信息	
						'selectSubject':this.selectsubject,
						'selectSubjectAndScore':this.selectsbujectandscore
						}
				}
				
				var self = this;
            	let url = api.apidomain+'/tqes/web/tchGrade/addGrade';
            	this.$http.post(url, gradedata).then(function(response) {
					if(response.data.success == true){
						self.loadingShow = false;
						self.$Message.success({
		                    content: '保存成功',
		                    duration: 10,
		                    closable: true
               			})
						//保存成功调用父组件的关闭面板函数
						self.$emit('chance')
	            }else if(response.data.success == false){
	            		self.loadingShow = false;
	            		self.$Message.error({
							content: response.data.msg,
							duration: 10,
							closable: true
						});	
	            	}
	            	
				})
				.catch(function(error) {
					self.loadingShow = false;
					self.$Message.error({
							content: '系统发生错误：错误码00006',
							duration: 10,
							closable: true
						});
				});
			},
			editgrade:function(){
				//如果等于空那么是新增年级，如果有的话则是编辑，拿id去取年级信息
				
				if(this.importeditid == ""){
					return;
				}else{
					this.loadingShow = true;
					var self = this;
	            	let url = api.apidomain+'/tqes/web/tchGrade/getGradeInfo';
	            	this.$http.post(url, {id:this.importeditid}).then(function(response) {
						if(response.data.success == true){
//							//console.log(response.data.data.score2)
							self.grade = response.data.data.no							//年级
							self.subject = 	response.data.data.isBranch					//文理科
							self.startgrade = response.data.data.enrolYear
							self.selectsubject = response.data.data.selectSubject		//选择上课的科目
							
							self.selectsbujectandscore = response.data.data.selectSubjectAndScore
							
							//判断score的值，控制满分值的显示和隐藏
							if(response.data.data.score1 != null){
//								//console.log("1")
								self.scorelistshow.splice(0,1,'show');
							}
							if(response.data.data.score2 != null){
//								//console.log("12")
								self.scorelistshow.splice(1,1,'show');
							}
							if(response.data.data.score3 != null){
//								//console.log("123")
								self.scorelistshow.splice(2,1,'show');
							}
							self.loadingShow = false;
							
		           }else if(response.data.success == false){
		            		self.$Message.error({
								content: response.data.msg,
								duration: 10,
								closable: true
							});
				            self.loadingShow = false;
		            	}
		            	
					})
					.catch(function(error) {
						self.$Message.error({
							content: '系统发生错误：错误码00007',
							duration: 10,
							closable: true
						});
						self.loadingShow = false;
					});
				}
			}
		},
		computed:{
			
		},
		watch:{
			scorelistshow:function(newValue, oldValue){
				let newval = newValue.toString()
				let arr = "show,show,show";
				if(newval == arr){
					this.btn1 = true;
				}else{
					this.btn1 = false;
				}
			},
			importeditid:function(){
				this.editgrade();
			},
//			
		}
	}
</script>

<style>
	.ivu-card-head{
		padding: 10px 16px;
	}
	.ivu-checkbox-group{
		display: inline;
	}
	input::-ms-input-placeholder{text-align: center;}
	input::-webkit-input-placeholder{text-align: center;}
</style>